package com.haofengsoft.platform.utils;

import java.lang.reflect.Method;
import java.util.List;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.telephony.TelephonyManager;

/**
 * Created by weiwei on 15/3/14.
 * 设备相关信息
 */
public class EquipmentUtil {
    // 获取ID号
    public static String deviceId(Context context) {
        try {
            final TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
            final String tmDevice, tmSerial, tmPhone, androidId;
            tmDevice = "" + tm.getDeviceId();
            tmSerial = "" + tm.getSimSerialNumber();
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class);
            String serialid = (String) get.invoke(c, "ro.serialno");
            //手机执行下面操作
//            androidId = "" + android.provider.Settings.Secure.getString(context.getContentResolver(), android.provider.Settings.Secure.ANDROID_ID);
//            UUID deviceUuid = new UUID(androidId.hashCode(), ((long) tmDevice.hashCode() << 32) | tmSerial.hashCode());
//            return deviceUuid.toString();

            //pad执行以下操作
            return serialid;
        } catch (Exception e) {
            //Serial Number
            return android.os.Build.SERIAL;
        }
    }

    // 获取手机型号
    public static String phoneModule() {
        return android.os.Build.MODEL;
    }

    // 获取系统版本
    public static String systemVersion() {
        return android.os.Build.VERSION.RELEASE;
    }

    //获取当前应用的版本号：

    public static int getVersionCode(Context context) {
        try {
            // 获取packagemanager的实例
            PackageManager packageManager = context.getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            int versionCode = packInfo.versionCode;
            return versionCode;
        } catch (Exception e) {
            return -1;
        }
    }
    //获取当前应用的版本号：

    public static String getVersionName(Context context) {
        try {
            // 获取packagemanager的实例
            PackageManager packageManager = context.getPackageManager();
            // getPackageName()是你当前类的包名，0代表是获取版本信息
            PackageInfo packInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
            String versionName = packInfo.versionName;
            return versionName;
        } catch (Exception e) {
            return "-1";
        }
    }
//    下面这段代码是我最新发现，无需权限，妥妥滴：

    /**
     * @param context
     * @return
     */
    public static boolean isBackground(Context context) {


        ActivityManager activityManager = (ActivityManager) context
                .getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) {
            if (appProcess.processName.equals(context.getPackageName())) {
                if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
//                    Log.i(String.format("Background App:", appProcess.processName));
                    //处于后台使用
                    return true;
                } else {
                    //处于当前使用
//                    Log.i(String.format("Foreground App:", appProcess.processName));
                    return false;
                }
            }
        }
        return false;
    }

}
